package pl.edu.agh.student.logic.bik;

import java.util.HashSet;
import java.util.Set;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;

import pl.edu.agh.student.logic.dao.BikObligationDao;
import pl.edu.agh.student.logic.service.ApplicationServiceImpl;
import pl.edu.agh.student.logic.util.EndpointProvider;
import pl.edu.agh.student.model.Application;
import pl.edu.agh.student.model.BikObligation;

@Name("bikService")
public class BikServiceImpl implements BikService {

	@In(value = "bikDao", required = false, create = true)
	@Out
	protected BikObligationDao bikObligationDao;
    
    @In(value = "applicationService", create = true, required = false)
    @Out
    protected ApplicationServiceImpl applicationService;
    
    @In("endpointProvider")
    EndpointProvider endpointProvider;
    
    public void getCustomerObligations(Object execution){
        String bikEndpoint = endpointProvider.getBikEndpoint();
        if(execution instanceof Long){
            Long id = (Long)execution;
            Application application = applicationService.find(id);
            try {
				getCustomerObligations(application, bikEndpoint);
			} catch (AxisFault e) {
				e.printStackTrace();
			}
        }
    }
            
	public Set<BikObligation> getCustomerObligations(Application application,
			String bikEndpoint) throws AxisFault {
		RPCServiceClient serviceClient;
		Set<BikObligation> obligations = new HashSet<BikObligation>();
//		try {
			serviceClient = new RPCServiceClient();

			Options options = serviceClient.getOptions();

			EndpointReference targetEPR = new EndpointReference(bikEndpoint);

			options.setTo(targetEPR);

			QName opGetBikReport = new QName(
					"http://service.bik.student.agh.edu.pl", "getReport");

			Object[] opGetBikReportArgs = new Object[] { application
					.getApplicant().getPesel() };
			Class[] returnTypes = new Class[] { BikReport.class };

			Object[] response = serviceClient.invokeBlocking(opGetBikReport,
					opGetBikReportArgs, returnTypes);

			BikReport result = (BikReport) response[0];

			if (result == null) {
				System.out.println("BikReport didn't initialize!");
				return null;
			}
			
			if(application.getBikObligations() != null){
				for(BikObligation bikObl : application.getBikObligations())
					bikObligationDao.delete(bikObl);
			}

			// Displaying the result
			for (String o : result.getObligations()) {
				System.out.println("Obligation: " + o);
				String[] info = o.split(",");
				BikObligation bo = new BikObligation(info[0], info[1],
						Double.parseDouble(info[2]), info[3]);
				bo.setApplication(application);
				bikObligationDao.saveOrUpdate(bo);
				System.out.println("BO: " + bo);
				obligations.add(bo);
			}

//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		return obligations;
	}
}